//  Author:
//       Daniel Großer <olav.daniel.grosser@googlemail.com>
//  
//  Copyright (c) 2012 Copyright (C) 2012 by Daniel Großer. This Code is under GNU LGPL 3.0
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
// 
//  You should have received a copy of the GNU Lesser General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;

namespace Beauty
{
    /// <summary>
    /// Use this attribute to mark delimeters. A delimeter is a terminal
    /// that very closely resembles a <see cref="Delimeter"/>, but it can
    /// be followed by any character. Examples for delimeters are
    /// braces, dots, colons, ... Just like keywords, the value of a
    /// delimeter is ignored because delimeters are only for the syntax,
    /// not for the semantics.
    /// </summary>
    /// <example>
    /// <code>
    /// [Delimeter("[")]
    /// public object LSBrace { get; set; }
    /// </code>
    /// </example>
    public sealed class DelimeterAttribute : TerminalAttribute
    {
        /// <summary>
        /// Create a new grammar part that matches a delimeter.
        /// </summary>
        /// <param name="keyword">The keyword to match.</param>
        public DelimeterAttribute(string delimeter)
        {
            StringTerminal = delimeter;
            Ignore = true;
        }

        /// <summary>
        /// Get the keyword modelled by this grammar part.
        /// </summary>
        public string Delimeter { get { return StringTerminal; } }

        /// <summary>
        /// Try to match the delimeter at the very start of a string.
        /// </summary>
        /// <param name="input">The string to match.</param>
        /// <returns>A value greater 0 iff match is successful, where the return value equals the matched length.</returns>
        public override int MatchStart(string input)
        {
            if (input.StartsWith(Delimeter, Comparison))
            {
                return Delimeter.Length;
            }
            else
                return 0;
        }
		
		/// <summary>
		/// Gets the scanner-precedence value. Terminals with a lower
		/// value will be preferred by the scanner. Among a set of
		/// equal-precedence terminals, the longest match will win.
		/// </summary>
		/// <value>
		/// 0, because delimeters have utmost priority.
		/// </value>
		public override byte ScannerPrecedence { get { return PRECEDENCE; } }

        /// <summary>
        /// Return a string representation.
        /// </summary>
        /// <returns>The string representation.</returns>
        public override string ToString()
        {
            return string.Format("Delimeter \"{0}\"", Delimeter);
        }
		
		/// <summary>
		/// The scanner precedence of Delimeter Terminals.
		/// </summary>
		public const byte PRECEDENCE = 0;		
		
    }
}
